<!-- 检查设备列表 -->
<template>
	<view class="page">
		<cu-custom bgColor="bg-white" :isBack="true">
			<block style="color: #777777;font-size: 32rpx;" slot="content">检查设备详情</block>
		</cu-custom>
		<scroll-view scroll-x class="bg-white nav">
			<view class="flex text-center">
				<view class="cu-item flex-sub" :class="index == TabCur ? 'text-orange cur' : ''" v-for="(item, index) in navs"
					:key="index" @tap="tabSelect" :data-id="index">
					{{  item  }}
					<!-- <u-badge :value="item.num" max="999" bgColor="#DC0000" :absolute="true" :offset="[14,8]"></u-badge> -->
				</view>
			</view>
		</scroll-view>
		<view style="margin: 20rpx 0;">
			<view class="cu-bar search bg-white">
				<view class="search-form">
					<text class="cuIcon-search"></text>
					<input v-model="name" :adjust-position="false" type="text" placeholder="请输入设备名称"
						confirm-type="search"></input>
				</view>
				<view class="action">
					<button @click="refresh" class="cu-btn bg-system-blue shadow-blur round">搜索</button>
				</view>
			</view>
		</view>
		<view v-if="!dataEmpty" :style="{ 'height': scrollHeight + 'px' }" style="overflow: hidden;">
			<movable-refresh ref="movableRefresh" :scrollHeight="scrollHeight" :noMore="noMore" :showGoTop="true"
				@refresh="refresh" @loadMore="loadMore" @="scrollTop">
				<view>
					<view class="content list-content" v-for="(item, index) in list" :key="index">
						<view class="pd">
							<text class="title">{{  item.name  }}</text>
							<p class="list-box">
								<text class="list-info">地点: </text>
								<text class="wbqy list-title">{{  item.address || '暂无' }}</text>
							</p>
							<p class="list-box">
								<text class="list-info">设备编码: </text>
								<text class="address list-title">{{  item.code || '暂无'  }}</text>
							</p>
							<p class="list-box">
								<text class="list-info">维保状态: </text>
								<text class="list-title">{{  statusFormat(item.status) || '暂无'  }}</text>
							</p>
							<p class="list-box">
								<view v-if="item.status == '01' && item.isvideo == '30' && item.mark" class="state-red">
									{{ item.mark }}
								</view>
							</p>
							<p class="list-box">
								<button v-if="item.status != '40'" class="bg-1" @click="reportHiddenDanger(item.ucId, item.equipmentId, item.maintenanceId)"
									:disabled="item.status == '40' || inspectData.status == '30'" type="primary" shape="square"
									>上报隐患</button>
								<button v-if="(item.isvideo=='01' || item.isvideo=='10') && item.status != '40'" class="bg-1" @click="saveVideo(item)"
									type="primary" shape="square"
									>拍摄视频</button>
								<button v-if="item.isvideo=='40' && item.status == '01'" class="bg-2" @click="sbFire(item)" type="primary" shape="square"
									>非测试</button>
								<button v-if="item.isvideo=='40' && item.status == '01'" class="bg-2" @click="wbCheck(item)" type="primary" shape="square"
								>测试</button>
							</p>
						</view>
					</view>
				</view>
			</movable-refresh>
		</view>
		<view v-if="dataEmpty" class="empty">
			<p>暂无数据</p>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
		<message />
	</view>
</template>

<script>
	import * as constants from '@/common/config.js';
import movableRefresh from '@/components/zyq-movableRefresh/zyq-movableRefresh.vue'
export default {
	components: {
		movableRefresh
	},
	onLoad(option) {
		this.maintenanceId = option.maintenanceId
		this.typeCode = option.typeCode
		this.typeId = option.typeId
		let system = uni.getSystemInfoSync()
		this.scrollHeight = system.windowHeight - system.statusBarHeight - 50
	},
	onShow() {
		if(!this.mark) {
			return
		}
		this.mark = false
		this.getDicts("equip_main_status", this).then(response => {
			this.statusOptions = response.data.data;
		});
		this.queryEquipmentType()
	},
	data() {
		return {
			navs: [],
			statusOptions: [],
			TabCur: 0,
			scrollLeft: 0,
			list: [],
			scrollHeight: 300,
			noMore: false,
			pageNum: 1,
			pageSize: 10,
			dataEmpty: false,
			maintenanceId: '',
			typeCode: '',
			typeId: '',
			name: null,
			urlvalue: '/wx/inspect/listJcData',
			mark: true,
			videoPath: '',
			serverVideoUri: ''
		}
	},
	methods: {
		saveVideo(item) {
			let _this = this
			uni.chooseVideo({
				count: 1,
				maxDuration: 15,
				sourceType: ['camera'],
				success: res => {
					_this.videoPath = res.tempFilePath;
					// 上传
					let app = getApp()
					let token = app.globalData.getToken('authorization')
					uni.uploadFile({
						url: constants.API + 'wx/common/upload',
						filePath: _this.videoPath,
						header: {
							"authorization": token
						},
						name: 'file',
						success: (uploadFileRes) => {
							_this.serverVideoUri = JSON.parse(uploadFileRes.data).data
							_this.jcUpdate(item)
						}
					});
				},
				fail: (err) => {
					_this.isupload = true
					uni.getSetting({
						success: (res) => {
							let authStatus = res.authSetting['scope.camera'];
							if (!authStatus) {
								uni.showModal({
									title: '授权失败',
									content: '需要从您的相机获取视频，请在设置界面打开相关权限',
									success: (res) => {
										if (res.confirm) {
											uni.openSetting()
										}
									}
								})
							}
						}
					})
				}
			});
		},
		jcUpdate(item){
			let that = this
			// 封装参数
			let params = {
				"ucId": item.ucId,
				"maintenanceId": item.maintenanceId,
				"menthd": 'jc',
				"videoUrl": that.serverVideoUri
			}
			// 请求数据
			let app = getApp()
			app.globalData.request({
				url: 'wx/maintenance/updateVideo',
				data: params
			}).then(function (res) {
				that.$refs.uToast.show({
					message: "视频拍摄成功",
					type: 'success',
					complete() {
						that.refresh()
					}
				})
				
			}).catch(function (e) {
			
			})
		},
		queryEquipmentType(){
			let that = this
			// 封装参数
			let params = {
				"ucId": that.typeId
			}
			// 请求数据
			let app = getApp()
			app.globalData.request({
				url: '/wx/inspect/queryEquipmentType',
				data: params
			}).then(function (res) {
				let datavo = res.data.data
				if (datavo.isVideo == '10' || datavo.isVideo == '20') {
					that.navs = ['未检测', '已检测']
				}else{
					that.navs = ['未检测', '已检测', '已接收信号']
				}
				that.refresh()
				
			}).catch(function (e) {
				uni.showToast({
					title: "请稍后再试",
					icon: 'none'
				})
			})
		},
		reportHiddenDanger(mainDataId, equipmentId,maintenanceId) {
			uni.navigateTo({
				url: '/wb/yhsb/yhsbym?ucId=' + maintenanceId + '&equipmentId=' + equipmentId + '&mainDataId=' + mainDataId + '&mark=jc'
			});
		},
		statusFormat(data) {
			return this.selectDictLabel(this.statusOptions, data);
		},
		tabSelect(e) {
			if(!this.mark) {
				return
			}
			this.mark = false
			this.TabCur = e.currentTarget.dataset.id;
			this.scrollLeft = (e.currentTarget.dataset.id - 1) * 60
			this.refresh()
		},
		scrollTop(scrollTop) {
			this.scrollTop = scrollTop
		},
		refresh() {
			this.dataEmpty = false
			this.pageNum = 1
			let that = this
			that.noMore = false
			let list = []
			
			// 封装参数
			let params = {
				"pageNum": that.pageNum,
				"pageSize": that.pageSize,
				"typeCode": that.typeCode,
				"maintenanceId": that.maintenanceId,
				"name": that.name
			}
			if (that.TabCur == 0) {
				params.status = '20'
			} else if (that.TabCur == 1) {
				params.status = '40'
			} else if (that.TabCur == 2) {
				params.status = '01'
			}
			// 请求数据
			let app = getApp()
			app.globalData.request({
				url: that.urlvalue,
				data: params
			}).then(function (res) {
				let datas = res.data.data.list
				let recors = []
				for (let i = 0; i < datas.length; i++) {
					recors.push(datas[i])
				}
				that.list = recors
				that.pageNum++
				if (res.data.data.pages === 1) {
					that.noMore = true
				}
				if (that.list.length == 0) {
					that.dataEmpty = true
				} else {
					that.dataEmpty = false
				}
				that.mark = true
			}).catch(function (e) {
				that.mark = true
				uni.showToast({
					title: "请稍后再试",
					icon: 'none'
				})
			})
		},
		loadMore() {
			if (this.noMore) {
				return
			}

			let list = this.list
			let that = this
			
			let params = {
				"pageNum": that.pageNum,
				"pageSize": that.pageSize,
				"typeCode": that.typeCode,
				"maintenanceId": that.maintenanceId,
				"name": that.name
			}
			if (that.TabCur == 0) {
				params.status = '20'
			} else if (that.TabCur == 1) {
				params.status = '40'
			} else if (that.TabCur == 2) {
				params.status = '01'
			}
			// 请求数据
			let app = getApp()
			app.globalData.request({
				url: that.urlvalue,
				data: params
			}).then(function (res) {
				let datas = res.data.data.list
				for (let i = 0; i < datas.length; i++) {
					list.push(datas[i])
				}
				that.list = list
				that.pageNum++
				if (res.data.data.pages === 1) {
					that.noMore = true
				}
				if (that.list.length == 0) {
					that.dataEmpty = true
				} else {
					that.dataEmpty = false
				}
				if (that.$refs.movableRefresh) {
					let that_ = that
					setTimeout(function() {
						that_.$refs.movableRefresh.endLoad() //刷新结束
					}, 1000)
				}
				that.mark = true
			}).catch(function (e) {
				that.mark = true
				uni.showToast({
					title: "请稍后再试",
					icon: 'none'
				})
			})
		},
	}
}
</script>

<style scoped>
.pd {
	background-color: #FFFFFF;
	margin: 20rpx 0;
	padding: 20rpx 0;
}

.pd text {
	display: flex;
	/* margin-left: 20rpx; */
	font-size: 24rpx;
	/* color: #151515; */
	letter-spacing: 0.39px;
	line-height: 50rpx;
}

.pd .title {
	font-size: 32rpx;
	color: #333333;
}

.pd .time {
	position: relative;
}

.pd .my-btn {
	position: absolute;
	right: 16px;
	margin-top: -40px;
}
.bg-1 {
	background: #FAB438 !important;
	color: #fff;
	height: 60rpx;
	line-height: 60rpx;
}

.bg-2 {
	background-color: #458DF5;
	color: #fff;
	height: 60rpx;
	line-height: 60rpx;
}
</style>
